package com.lzy.crm.controller;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lzy.crm.util.UserConstants;



/**
 * 登录验证码
 * @author LIANGXIONG
 * 2015-01-12
 */
@Controller
@RequestMapping("/validateCode")
public class ValidateCodeController{

	
	Logger logger= Logger.getLogger(ValidateCodeController.class);
	
	public ValidateCodeController() {
		super();
	}

	public Color getRandColor(int s, int e) {
		Random random = new Random();
		if (s > 255) s = 255;
		if (e > 255) e = 255;
		int r = s + random.nextInt(e - s);
		int g = s + random.nextInt(e - s);
		int b = s + random.nextInt(e - s);
		return new Color(r, g, b);
	}
 
	@RequestMapping("getValidateCode")
	public void getValidateCode(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			//禁止缓存
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "No-cache");
			response.setDateHeader("Expires", 0);
			// 指定生成的响应是图片
			response.setContentType("image/jpeg");
			int width = 126;
			int height = 44;
			BufferedImage image = new BufferedImage(width, height,
			BufferedImage.TYPE_INT_RGB);    //创建BufferedImage类的对象
			Graphics g = image.getGraphics();     //创建Graphics类的对象
			Graphics2D g2d=(Graphics2D)g;      //通过Graphics类的对象创建一个Graphics2D类的对象
			Random random = new Random();      //实例化一个Random对象
			Font mFont = new Font("华文宋体", Font.BOLD, 30);  //通过Font构造字体
			g.setColor(getRandColor(200, 250));     //改变图形的当前颜色为随机生成的颜色
			g.fillRect(0, 0, width , height);     //绘制一个填色矩形

			//画一条折线  
			BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL); //创建一个供画笔选择线条粗细的对象
			g2d.setStroke(bs);     //改变线条的粗细
			g.setColor(Color.DARK_GRAY);  //设置当前颜色为预定义颜色中的深灰色
			int[] xPoints=new int[3];
			int[] yPoints=new int[3];
			for(int j=0;j<3;j++){
				xPoints[j]=random.nextInt(width - 1);
				yPoints[j]=random.nextInt(height - 1);
			}
			g.drawPolyline(xPoints, yPoints,3);
			//生成并输出随机的验证文字  
			g.setFont(mFont);  
			String sRand="";
			int itmp=0;
			for(int i=0;i<4;i++){
				if(random.nextInt(2)==1){
					itmp=random.nextInt(26)+65; //生成A~Z的字母
				}else{
					itmp=(int)Math.round(Math.random()*(9-1)+1)+48; //生成1~9的数字
				}
				char ctmp=(char)itmp;
				sRand+=String.valueOf(ctmp);
				Color color=new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110));
				g.setColor(color);
				/****随机缩放文字并将文字旋转指定角度**/
				//将文字旋转指定角度
				Graphics2D g2d_word=(Graphics2D)g;
				AffineTransform trans=new AffineTransform();
				//trans.rotate(random.nextInt(45)*3.14/180,15*i+10,7);
				//缩放文字
				float scaleSize=random.nextFloat()+1.0f;
				if(scaleSize>0.6f) scaleSize=0.5f;
				trans.scale(scaleSize, scaleSize+0.5);
				g2d_word.setTransform(trans);
				/************************/
				g.drawString(String.valueOf(ctmp),30*i+40,36);

			}
			//将生成的验证码保存到Session中
			request.getSession().setAttribute(UserConstants.VERIFY_CODE_PREFIX, sRand);
			//SessionManager.addVerifyCode(request, sRand);
			g.dispose();
			ImageIO.write(image,"JPEG",response.getOutputStream());
		}catch(Exception e){
			logger.error(e);
		}
	}

	/*@ResponseBody
	@RequestMapping("existVerifyCode")
	public String existVerifyCode(HttpServletRequest request,String verifyCode){
		BaseParamResp resp = new BaseParamResp();
		if(StringUtil.isEmpty(verifyCode)){
			resp.addError(request, ErrorMsgConst.PARAM_NOT_NULL);
			return JsonUtil.toJson(resp);
		}else{
			String code = SessionManager.getVerifyCode(request);
			if(!StringUtil.equalsIgnoreCase(verifyCode, code)){
				resp.addError(request, ErrorMsgConst.VERIFY_CODE_ERROR);
			}
		}
		if(resp.isError())
			SessionManager.delVerifyCode(request);
		return JsonUtil.toJson(resp);
	}*/
	
	public static void main(String[] args) {
		/*String name="";
		Random random = new Random(); // 实例化一个Random对象
		int itmp=0;
		int length=20;//默认的文件长度是20
		for (int i = 0; i <length ; i++) {
			System.out.println(random.nextInt(2));
			if (random.nextInt(2) == 1) {
				itmp = random.nextInt(26) + 65; // 生成A~Z的字母
			} else {
				itmp = random.nextInt(10) + 48; // 生成0~9的数字
			}
			char ctmp = (char) itmp;
			name += String.valueOf(ctmp);
		}
		System.out.println(name);*/
		
		for(int i=0;i<200;i++){
			int a = (int)Math.round(Math.random()*(9-1)+1);
		    System.out.print(a+" ");
		}
	}
} 
